package com.qf.gh.service.impl;

import com.qf.gh.dao.AdminDao;
import com.qf.gh.dao.impl.AdminDaoImpl;
import com.qf.gh.domain.Goods;
import com.qf.gh.domain.PageBean;
import com.qf.gh.domain.User;
import com.qf.gh.service.AdminService;
import com.qf.gh.utils.TxUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class AdminServiceImpl implements AdminService {
    AdminDao adminDao=new AdminDaoImpl();
    @Override
    public User login(String username, String password) {
        return adminDao.select(username,password);
    }

    @Override
    public List<User> select(int i) {
        return adminDao.selectUser(i);
    }

    @Override
    public List<User> selectOne(String where, List<Object> parmas) {
        return adminDao.selectOne(where,parmas);
    }

    @Override
    public void delete(int parseInt) {

    }



    @Override
    public List<User> selectDelete(int parseInt) {
        Connection conn = TxUtils.getConn();
        try {
            conn.setAutoCommit(false);
            adminDao.delete(parseInt);
            List<User> user = adminDao.selectUser(1);
            conn.commit();
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } finally {
            TxUtils.remove();
            try {
                conn.setAutoCommit(true);
                conn.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public List<User> selectAllow(int parseInt) {
        Connection conn = TxUtils.getConn();
        try {
            conn.setAutoCommit(false);
            adminDao.allow(parseInt);
            List<User> user = adminDao.selectUser(2);
            conn.commit();
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } finally {
            TxUtils.remove();
            try {
                conn.setAutoCommit(true);
                conn.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public List<User> selectValidOne(String toString, List<Object> parmas, int i) {
        return adminDao.selectValidOne(toString,parmas,i);
    }

    @Override
    public PageBean findByPage(int pageNum1, int pageSize1) {
        PageBean pageBean=new PageBean();
        pageBean.setPageNum(pageNum1);
        pageBean.setPageSize(pageSize1);
        long totalCount = adminDao.selectUserCount(1);
        pageBean.setTotalCount(totalCount);
        List<User> list = adminDao.findData((pageNum1 - 1) * pageSize1 , pageSize1);
        pageBean.setData(list);

        return pageBean;
    }
}
